package com.fragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


import com.activities.Zhengxiangdi_items;
import com.bean.ZXfeeds;
import com.example.lizhiinformation.R;
import com.me.maxwin.view.XListView;
import com.me.maxwin.view.XListView.IXListViewListener;
import com.myadapter.ZhenXiangDiadapter;
import com.ownload.Httputil;
import com.ownload.Httputil.OnNetWorkRespones;
import com.tools.ZhenXiangDiJson;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

@SuppressLint("ValidFragment")
public class Fragment_Zhengxiangdi extends Fragment implements
	IXListViewListener {
	private String path1;
	private String path2;
	private String itemspath;
	private ZhenXiangDiadapter adapter;
	private ListView listview;
	private int code=1;
	public Fragment_Zhengxiangdi(String path1, String path2, String itemspath) {
		super();
		this.path1 = path1;
		this.path2 = path2;
		this.itemspath = itemspath;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.zhenxiangdi, null);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		listview = (ListView) view.findViewById(R.id.zhenxiangdilistview);
		listview.setVisibility(View.GONE);
		((XListView) listview).setXListViewListener(this);
		((XListView) listview).setPullLoadEnable(true);
		listviewchick();
		setlistview();
		
	}
/**
 * listviewitems的点击事件
 */
	public void listviewchick(){
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(getActivity(),Zhengxiangdi_items.class);
				intent.putExtra("url", adapter.getItem(arg2));
				startActivity(intent);
			}
		});
	}
	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		setlistview();
		onLoad();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		code++;
		addlistview(code);
		onLoad();
	}
	/**
	 * listview数据加载
	 */
	public void setlistview() {
		Httputil.OnWorkDown(path1 + "1" + path2, new OnNetWorkRespones() {

			@Override
			public void OK(String respose) {
				// TODO Auto-generated method stub
				ArrayList<ZXfeeds> json = new ZhenXiangDiJson().json(respose);
				adapter = new ZhenXiangDiadapter(json, getActivity(),listview);
				listview.setAdapter(adapter);
				listview.setVisibility(View.VISIBLE);
			}

			@Override
			public void ERROR(String error) {
				// TODO Auto-generated method stub

			}
		});

	}
	/**
	 * listview数据加载刷新
	 * @param code
	 */
	public void addlistview(int code) {
		Httputil.OnWorkDown(path1 + String.valueOf(code) + path2,
				new OnNetWorkRespones() {

					@Override
					public void OK(String respose) {
						// TODO Auto-generated method stub
						ArrayList<ZXfeeds> json = new ZhenXiangDiJson()
								.json(respose);
						
						adapter.addData(json);
					}

					@Override
					public void ERROR(String error) {
						// TODO Auto-generated method stub

					}
				});
	}

	// 停止加载和刷新动画
	private void onLoad() {
		((XListView) listview).stopRefresh();
		((XListView) listview).stopLoadMore();
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日      HH:mm ");
		long millis = System.currentTimeMillis();
		Date date = new Date(millis);
		String format = sdf.format(date);
		((XListView) listview).setRefreshTime(format);
		

	}
}
